//错误监听方式：https://blog.csdn.net/m0_55049655/article/details/148286170?spm=1001.2014.3001.5501
        //1.try...catch捕捉同步任务
        //2.promise中错误：：catch（onRejected）/unhandledRejection/async await + try...catch
        //3.全局错误:window.onerror,但无法处理资源加载错误
        //4.资源加载错误：window.addEventListener('error',(e)=>{})
export default function error(){
    //资源加载错误：js、css、img等
    window.addEventListener('error',(e)=>{
        const target = e.target as HTMLImageElement & HTMLScriptElement & HTMLLinkElement;
        if(target.src||target.href){
            const url= target.src||target.href;
            const reportData={
                type: 'error',
                subType: 'resource',
                url,
                html: target.outerHTML,
                pageUrl: window.location.href,
                pahts: (e as unknown as { path: HTMLElement[] }).path?.map(el => el.outerHTML),
            }

            //TODO: 添加错误上报
        }
    },true)


    //全局错误
    window.onerror=function(msg,url,lineNo,columnNo,error){
        const reportData = {
            type: 'error',
            subType: 'js',
            msg,
            url,
            lineNo,
            columnNo,
            stack: error?.stack,
            pageUrl: window.location.href,
            startTime: performance.now(),
        };
        //TODO: 上报 reportData
    }


    //捕获与上传promise中错误
    window.addEventListener( 'unhandledrejection', function (e) { 
        const reportData = {
            type: 'error',
            subType: 'promise',
            reason: e.reason?.stack,
            pageUrl: window.location.href,
            startTime: e.timeStamp,
        };
        //TODO:上报错误
    },true)

}